/**
 *
 */
package com.googlecode.bip.entity;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.TimeZone;
import cx.ath.choisnet.sql.SQLTools;

/**
 * Entry for 'users' table
 *
 * @author Claude CHOISNET
 */
public class User implements IUser
{
    private static final long serialVersionUID = 1L;
    private int     iduser;
    private String  username;
    private String  password; // TODO: REMOVE THIS !
    private int     isactive;
    private String  idlocal;
    private TimeZone timeZone;

    /**
     * Create a NOT initialized User
     */
    public User()
    {
    }

    /**
     * Create a shadow copy of giving User
     *
     * @param o User to clone
     */
    public User( final User o )
    {
        this.iduser   = o.iduser;
        this.username = o.username;
        this.password = o.password;
        this.isactive = o.isactive;
        this.idlocal  = o.idlocal;
        this.timeZone = o.timeZone;
    }

    /**
     *
     * @param rSet
     * @return
     * @throws SQLException
     */
    public User set( final ResultSet rSet ) throws SQLException
    {
        this.iduser   = rSet.getInt( "iduser" );
        this.username = rSet.getString( "username" );
        this.password = rSet.getString( "password" );
        this.isactive = rSet.getInt( "isactive" );
        this.idlocal  = rSet.getString( "idlocal" );

        String timezoneid = rSet.getString( "timezone" );

        if( timezoneid == null ) {
            this.timeZone = null;
            }
        else {
            this.timeZone = TimeZone.getTimeZone( timezoneid );
            }

        return this;
    }

    /**
     * Returns the iduser for this user (ReadOnly)
     * @return the iduser
     */
    @Override
    public int getIdUser()
    {
        return iduser;
    }

    /**
     * <p>ReadOnly</p>
     *
     * @param iduser the iduser to set
     */
    public void protected_setIdUser( int iduser )
    {
        this.iduser = iduser;
    }

    /**
     * Returns the username for this user
     * @return the username
     */
    @Override
    public String getUserName()
    {
        return username;
    }

    /**
     * Normalized, validate then set the username for this user
     * @param username the username to set
     */
    @Override
    public void setUsername( String username )
    {
        username = username.trim().toLowerCase();

        if( username.length() == 0 && username.length() > 63 ) {
            throw new IllegalArgumentException( "Bad length: " + username.length() );
            }
        if( ! EntityHelper.isValidAlphanumericNoWhiteSpaces( username ) ) {
            throw new IllegalArgumentException( "Bad content" );
        }

        //TODO: improve check

        this.username = username;
    }

    /**
     * <p>WriteOnly</p>
     * Juste pour que tu permettre contruire ta maquette
     * @return the password
     */
    public String getPassword() // TODO: REMOVE THIS
    {
        return password;
    }

    /**
     * Juste pour que tu permettre contruire ta maquette (cette method doit dispatre)
     *
     * @param password the password to set
     */
    @Override
    public void setPassword( String password ) // TODO: REMOVE THIS
    {
        //TODO: check content
        this.password = password;
    }

    /**
     * Set the password for this user (WriteOnly)
     *
     * @param oldPassword the previous password (could be null)
     * @param password the password to set (could not be null)
     */
    @Override
    public void setPassword(
            final String oldPassword,
            final String password
            )
    {
        // TODO: check value password

        // check oldPassword
        // TODO: check oldPassword

        // if ok set new password
        this.password = password;
    }

    /**
     * @return the isactive
     */
    @Override
    public int getIsActive()
    {
        return isactive;
    }

    /**
     * @param isactive the isactive to set
     */
    @Override
    public void setIsActive( int isactive )
    {
        // TODO: check value
        this.isactive = isactive;
    }

    /**
     * @return the idlocal
     */
    @Override
    public String getIdLocal()
    {
        return idlocal;
    }

    /**
     * @param idlocal the idlocal to set
     */
    @Override
    public void setIdlocal( String idlocal )
    {
        // TODO: check value
        this.idlocal = idlocal;
    }

    @Override
    public TimeZone getTimeZone()
    {
        return this.timeZone;
    }

    /**
     * TODO doc
     * @return
     */
    public String formatTimeZone()
    {
        if( this.timeZone != null ) {
            return SQLTools.parseFieldValue( this.timeZone.getID() );
            }
        else {
            return null;
            }
    }

    @Override
    public void setTimeZone( TimeZone tz )
    {
        this.timeZone = tz;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString()
    {
        StringBuilder builder = new StringBuilder();

        builder.append( "User [iduser=" ).append( iduser )
               .append( ", username=" ).append( username )
               .append( ", isactive=" ).append( isactive )
               .append( ", idlocal=" ).append( idlocal )
               .append( ", timeZone=" ).append( timeZone )
               .append( "]" );

        return builder.toString();
    }

}
